<template>
	<view class="activity-area">
		<view class="subsection-container">
			<u-subsection 
				:list="['未开始', '进行中', '已结束']" 
				:current="current" 
				@change="handleSubsectionChange" 
				:fontSize="14"
				:activeColor="activeColor"
				:bold="true"
				:barWidth="40"
				:barHeight="4"
				:barColor="activeColor"
			></u-subsection>
		</view>
		
		<view class="activity-list">
			<u-loadmore :status="loading ? 'loading' : 'nomore'" v-if="loading || activityList.length === 0" />
			<view class="activity-item" v-for="(item, index) in activityList" :key="index">
				<image class="activity-img" :src="item.url || defaultImage" mode="aspectFill" />
				<view class="activity-info">
					<view class="activity-title">{{ item.title.length > 15 ? item.title.slice(0, 15) + ' . . .' : item.title }}</view>
					<view class="activity-desc">{{ item.description.length > 20 ? item.description.slice(0, 18) + ' . . .' : item.description }}</view>
					<view class="activity-time">{{ formatDateTime(item.startTime) }} - {{ formatDateTime(item.endTime) }}</view>
					<view class="activity-participants">
						<u-icon name="account" size="24" color="#2979ff" />
						<text class="participants-count">{{ item.currentParticipants }}/{{ item.maxParticipants }}</text>
					</view>
					<view class="signup-btn" v-if="current === 0" @click="handleSignup(item)">立即报名</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getNotStartActivity, getStartActivity, getEndActivity } from '@/api/activity.js'
	
	export default {
		data() {
			return {
				current: 0,
				loading: false,
				activityList: [],
				defaultImage: '/static/img/ai.jpg',
				activeColor: '#2979ff'
			}
		},
		onLoad() {
			this.loadActivityList()
		},
		methods: {
			formatDateTime(dateTimeStr) {
				if (!dateTimeStr) return ''
				// 去掉年份和秒，格式：MM-DD HH:mm
				return dateTimeStr.replace(/^\d{4}-/, '').replace(/:\d{2}$/, '')
			},
			async loadActivityList() {
				this.loading = true
				try {
					const campusId = uni.getStorageSync('campusId');
					let res
					switch(this.current) {
						case 0:
							res = await getNotStartActivity(campusId)
							break
						case 1:
							res = await getStartActivity(campusId)
							break
						case 2:
							res = await getEndActivity(campusId)
							break
					}
					
					if (res && res.data) {
						this.activityList = res.data.list || []
					}
				} catch (e) {
					console.error('加载活动列表失败:', e)
					uni.showToast({
						title: '加载失败，请重试',
						icon: 'none'
					})
				} finally {
					this.loading = false
				}
			},
			handleSubsectionChange(index) {
				this.current = index
				this.loadActivityList()
			},
			handleSignup(item) {
				// TODO: 处理报名逻辑
				uni.showToast({
					title: '报名功能开发中',
					icon: 'none'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.activity-area {
	min-height: 100vh;
	background: #f7f7f7;
	padding: 20rpx;
}

.subsection-container {
	background: #fff;
	padding: 20rpx;
	border-radius: 12rpx;
	margin-bottom: 20rpx;
	box-shadow: 0 2rpx 8rpx rgba(41, 121, 255, 0.1);
}

.activity-list {
	.activity-item {
		display: flex;
		background: #fff;
		border-radius: 12rpx;
		margin-bottom: 20rpx;
		overflow: hidden;
		box-shadow: 0 2rpx 8rpx rgba(41, 121, 255, 0.1);
	}
	
	.activity-img {
		width: 200rpx;
		height: 200rpx;
		background: #eee;
		flex-shrink: 0;
	}
	
	.activity-info {
		flex: 1;
		padding: 15rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		position: relative;
	}
	
	.activity-title {
		font-size: 28rpx;
		font-weight: 500;
		color: #333;
	}
	
	.activity-desc {
		font-size: 24rpx;
		color: #666;
		margin: 8rpx 0;
	}
	
	.activity-time {
		font-size: 22rpx;
		color: #666;
		margin-bottom: 8rpx;
	}
	
	.activity-location {
		font-size: 22rpx;
		color: #666;
		margin-bottom: 8rpx;
	}
	
	.activity-participants {
		display: flex;
		align-items: center;
		
		.participants-count {
			font-size: 22rpx;
			color: #666;
			margin-left: 8rpx;
		}
	}
	
	.signup-btn {
		background-color: #2979ff;
		color: #fff;
		font-size: 28rpx;
		padding: 12rpx 40rpx;
		border-radius: 40rpx;
		position: absolute;
		right: 15rpx;
		bottom: 15rpx;
	}
}
</style>
